#include <iostream>
#include "DoubleLinkList.h"
#include "LinkStack.h"

// 表达式求值
// （1+2）*3 = ？

// 判断是否是运算符
bool IsOper(char c)
{
    switch(c)
    {
        case '+':
        case '-':
        case '*':
        case '/':
        case '(':
        case ')':
        case '=':
            return true;
        default:
            return false;
    }
}

// 判断是否是数字
bool IsNum(char c)
{
    if(c >= '0' && c <= '9')
        return true;
    return false;
}

int Operation(int prev, int next, char symbol)
{
    switch(symbol)
    {
        case '+':return prev + next;break;
        case '-':return prev - next;break;
        case '*':return prev * next;break;
        case '/':
            if(next == 0)
            {
                printf("除以0!\n");
                exit(-1); // 退出程序
            }
            return prev / next;break;
        default:
            exit(-1);
    }
}

// 计算优先级
char Precede(char ch1, char ch2)
{
    int row = 0, column = 0;
    switch(ch1)
    {
        case '+':row = 0;break;
        case '-':row = 1;break;
        case '*':row = 2;break;
        case '/':row = 3;break;
        case '(':row = 4;break;
        case ')':row = 5;break;
        case '=':row = 6;break;
        default:
            break;
    }

    switch(ch2)
    {
        case '+':column = 0;break;
        case '-':column = 1;break;
        case '*':column = 2;break;
        case '/':column = 3;break;
        case '(':column = 4;break;
        case ')':column = 5;break;
        case '=':column = 6;break;
        default:
            break;
    }

    char pre[7][7] = 
    {
        {'>', '>', '<', '<', '<', '>', '>'},
        {'>', '>', '<', '<', '<', '>', '>'},
        {'>', '>', '>', '>', '<', '>', '>'},
        {'>', '>', '>', '>', '<', '>', '>'},
        {'<', '<', '<', '<', '<', '=', '0'},
        {'>', '>', '>', '>', '0', '>', '>'},
        {'<', '<', '<', '<', '<', '0', '='},
    };
    return pre[row][column];
}

int main()
{
    LinkStack NumS;  // 数据栈
    LinkStack OperS; // 运算符栈
    OperS.Push('=');

    char str[64] = {0};
    // 读到'\n'才结束
    scanf("%[^\n]", str);

    int i = 0; 
    while(str[i] != '\0')
    {
        // 数字
        if(IsNum(str[i]))
        {
            int num = 0;
            while(IsNum(str[i]))
            {
                num = 10 * num + str[i] - '0';
                i++;
            }
            std::cout<<"num : "<<num<<std::endl;
            NumS.Push(num);
        }
        else if(IsOper(str[i]))
        {
            char pre = Precede (OperS.GetTop(), str[i]);
            switch(pre)
            {
                case '>':
                {
                    int next = NumS.GetTop();
                    NumS.Pop();
                    int prev = NumS.GetTop();
                    NumS.Pop();
                    char op = OperS.GetTop();
                    OperS.Pop();
                    NumS.Push(Operation(prev, next, op));
                    break;
                }
                case '<':
                {
                    OperS.Push(str[i]);
                    i++;
                    break;
                }
                case '=':
                {
                    OperS.Pop();
                    i++;
                    break;
                }
                default:
                {
                    std::cout<<"表达式有误!\n";
                    exit(-1);
                    break;
                } 
            }
        }
        else if(str[i] == ' ')
        {
            while(str[i] == ' ')
                i++;
        }
    }
    
    std::cout<<"答案是："<<NumS.GetTop()<<std::endl;
    
    return 0;
}